package com.cloudbroker.bcs.common.mq.rabbitmq;

import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.core.AmqpAdmin;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;

import com.cloudbroker.bcs.common.configure.ConfigUtil;
import com.cloudbroker.bcs.common.util.ValueUtil;

public class ChannelManager {
    
    public static final String PROP_KEY_ADDRESSES = "addresses";
    
    public static final String PROP_KEY_USERNAME = "username";
    
    public static final String PROP_KEY_PASSWORD = "password";
    
    public static final String PROP_KEY_VIRTUAL_HOST = "virtualHost";
    
    public static final String PROP_KEY_CHANNEL_CACHE_SIZE = "channelCacheSize";
    
    public static ConnectionFactory createConnectionFactory(String addresses, String username, String password,
            String virtualHost) {
        return createConnectionFactory(addresses, username, password, virtualHost, 1);
    }
    
    public static ConnectionFactory createConnectionFactory(String addresses, String username, String password,
            String virtualHost, int channelCacheSize) {
        CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
        connectionFactory.setAddresses(addresses);
        connectionFactory.setUsername(username);
        connectionFactory.setPassword(password);
        connectionFactory.setVirtualHost(virtualHost);
        connectionFactory.setChannelCacheSize(channelCacheSize);
        return connectionFactory;
    }
    
    @SuppressWarnings("unchecked")
    public static ConnectionFactory createConnectionFactoryFromConfigUtil(String connectionName) {
        if (StringUtils.isBlank(connectionName)) {
            throw new RuntimeException("connectionName is required");
        }
        Map<String, Object> config = (Map<String, Object>) ConfigUtil.getProp().get(connectionName);
        if (null == config) {
            throw new RuntimeException("undefined connectionName: " + connectionName);
        }
        String addresses = ValueUtil.getString(config.get(PROP_KEY_ADDRESSES));
        if (StringUtils.isBlank(addresses)) {
            throw new RuntimeException("addresses is required");
        }
        String username = ValueUtil.getString(config.get(PROP_KEY_USERNAME));
        if (StringUtils.isBlank(username)) {
            throw new RuntimeException("username is required");
        }
        String password = ValueUtil.getString(config.get(PROP_KEY_USERNAME));
        if (StringUtils.isBlank(password)) {
            throw new RuntimeException("password is required");
        }
        String virtualHost = ValueUtil.getString(config.get(PROP_KEY_VIRTUAL_HOST));
        if (StringUtils.isBlank(virtualHost)) {
            throw new RuntimeException("virtualHost is required");
        }
        int channelCacheSize = ValueUtil.getInt(config.get(PROP_KEY_CHANNEL_CACHE_SIZE), 1);
        return createConnectionFactory(addresses, username, password, virtualHost, channelCacheSize);
    }
    
    public static AmqpAdmin createAmqpAdmin(ConnectionFactory connectionFactory) {
        return new RabbitAdmin(connectionFactory);
    }
    
    public static Queue createQueue(AmqpAdmin amqpAdmin) {
        return amqpAdmin.declareQueue();
    }
    
    public static String createQueue(AmqpAdmin amqpAdmin, Queue queue) {
        return amqpAdmin.declareQueue(queue);
    }
    
}
